using System;
using System.IO;

public class UeProjPojo
{
  // 原始路径
  public string orgPath { get; set; }

  // root
  public string rootPath { get; set; }

  // project name
  public string projectName { get; set; }

  public string projectSourcePublicPath { get; set; }

  public string projectSourcePrivatePath { get; set; }


  public string sourcePublicPath { get; set; }

  public string sourcePrivatePath { get; set; }

  // 宏名
  public string macroName { get; set; }

  public Param param { get; set; }

  private UeProjDate ct = new();

  public UeProjPojo(string path, Param param, Func<UeProjPojo, string> GetMacroName)
  {
    this.orgPath = path;
    this.projectName = PathUtils.GetDirName(path);
    this.rootPath = PathUtils.GetDirPath(path);
    this.projectSourcePublicPath = Path.Combine(this.rootPath, ct.Source, this.projectName, ct.Public);
    this.projectSourcePrivatePath = Path.Combine(this.rootPath, ct.Source, this.projectName, ct.Private);
    this.sourcePublicPath = this.buildSourcePublicPath();
    this.sourcePrivatePath = this.buildSourcePrivatePath();
    this.param = param;

    // last
    this.macroName = GetMacroName(this);
  }

  private string buildSourcePublicPath()
  {
    if (!this.projectName.Equals(ct.Source) || !Directory.Exists(this.rootPath)) return null;
    return this.buildSourcePath(this.rootPath, ct.Public);
  }

  private string buildSourcePrivatePath()
  {
    if (!this.projectName.Equals(ct.Source) || !Directory.Exists(this.rootPath)) return null;
    return this.buildSourcePath(this.rootPath, ct.Private);
  }

  private string buildSourcePath(string currentPath, string filename)
  {
    try
    {
      foreach (var dir in Directory.GetDirectories(currentPath))
      {
        var dirName = Path.GetFileName(dir);
        if (dirName.Equals(filename))
        {
          return dir;
        }
      }

      foreach (var dir in Directory.GetDirectories(currentPath))
      {
        var foundPath = buildSourcePath(dir, filename);
        if (foundPath != null)
        {
          return foundPath; // 如果子目录中找到了public文件夹，立即返回
        }
      }
    }
    catch (Exception) { }
    return null;
  }
}

public record UeProjDate(
  string Source = "Source",
  string Public = "Public",
  string Private = "Private"
)
{
}
